<HTML>
<HEAD>
<TITLE>Hattrick weather statistics</TITLE>
</HEAD>
<BODY>

<STYLE>
TR {
	text-align: center;
	font-family: sans-serif;
	font-size: 12;
}
TD.o0 {
	background-color: #B0FFB0;
}
TD.o1 {
	background-color: #FFFFB0;
}
TD.o2 {
	background-color: #FFD0B0;
}
TD.o3 {
	background-color: #FFB0B0;
}
</STYLE>

<?php

require_once("track.inc.php");
require_once("database.inc.php");

hit();
$dbh = connect_db();

$weathers = mysql_query("SELECT today.weatherid, yesterday.tomorrowweatherid, count(*) FROM maptrick yesterday, maptrick today WHERE yesterday.scanid = today.scanid-1 and (today.scanid % 7 = 0) and yesterday.regionid = today.regionid GROUP BY today.weatherid, yesterday.tomorrowweatherid", $dbh);
$weather = array();
$sumtoday = array();
$sumtomorrow = array();
$sumtotal = 0;

while ($row = mysql_fetch_array($weathers)) {
	$weather[$row[0]][$row[1]] = $row[2];
	$sumtoday[$row[0]] += $row[2];
	$sumtomorrow[$row[1]] += $row[2];
	$sumtotal += $row[2];
}

echo "<TABLE BORDER=\"1\">\n";
echo "\t<TR><TD ROWSPAN=\"2\" COLSPAN=\"2\"></TD><TD COLSPAN=\"5\">Prediction (Sundays)</TD></TR>\n";
echo "\t<TR><TD><IMG SRC=\"weather0.gif\"></TD><TD><IMG SRC=\"weather1.gif\"></TD><TD><IMG SRC=\"weather2.gif\"></TD><TD><IMG SRC=\"weather3.gif\"></TD><TD>Total</TD></TR>\n";
echo "<TR><TD ROWSPAN=\"5\">Actual<BR/>(Mondays)</TD>";

$off_by = array();

foreach (array_keys($weather) as $today) {
	if ($today != 0) echo "\t<TR>";
	echo "<TD><IMG SRC=\"weather$today.gif\"></TD>";
	foreach (array_keys($weather[$today]) as $tomorrow) {
		$totperc = round(($weather[$today][$tomorrow]/$sumtotal)*100,1);
		$delta = abs($today - $tomorrow);
		$off_by[$delta]+=$weather[$today][$tomorrow];
		echo "<TD CLASS=\"o$delta\">{$weather[$today][$tomorrow]}<BR/>($totperc%)</TD>";
	}
	$totperc = round(($sumtoday[$today] / $sumtotal)*100, 1);
	echo "<TD>{$sumtoday[$today]}<BR/>($totperc%)</TD></TR>\n";
}
echo "\t<TR><TD>Total</TD><TD>{$sumtomorrow[0]}<BR/>(" . round(($sumtomorrow[0] / $sumtotal)*100,1) . "%)</TD><TD>{$sumtomorrow[1]}<BR/>(" . round(($sumtomorrow[1] / $sumtotal)*100,1) . "%)</TD><TD>{$sumtomorrow[2]}<BR/>(" . round(($sumtomorrow[2] / $sumtotal)*100,1) . "%)</TD><TD>{$sumtomorrow[3]}<BR/>(" . round(($sumtomorrow[3] / $sumtotal)*100,1) . "%)</TD><TD><B>$sumtotal<BR/>(100%)</B></TD></TR>\n";

echo "</TABLE>\n";
echo "<BR/>The numbers indicated are the number of times that specific situation occurred and was measured, and the relative percentage to the total ($sumtotal).<BR/><BR/>\n\n";

for ($i = 0; $i <= 3; $i++) {
	$offperc = round(($off_by[$i] / $sumtotal)*100,1);
	echo "The prediction was off by $i in {$off_by[$i]} cases ($offperc%).<BR/>\n";
}

echo "</BODY>\n";
echo "</HTML>\n";

?>
